package com.example.yls.demoa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private long lastPressTime = 0;
    private final String TAG = "MainActivity";
    private ListView nameListView;
//    private String[] nameArr = {"狼狗","二哈","藏獒","二狗","狼狗","二哈","藏獒","二狗","狼狗","二哈","藏獒","二狗","狼狗","二哈","藏獒","二狗","狼狗","二哈","藏獒","二狗"};

    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private MyAdapter adapter;
    private Button btnAddStu;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化学生列表
        initStuList();

        nameListView = findViewById(R.id.name_listview);

        //自定义适配器
        adapter = new MyAdapter(MainActivity.this, studentArrayList);

        nameListView.setAdapter(adapter);

        btnAddStu =findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,AddActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initStuList() {
        Student s1 = new Student(R.drawable.dog1, "大狗", 18);
        Student s2 = new Student(R.drawable.dog2, "二狗", 19);
        Student s3 = new Student(R.drawable.dog3, "三狗", 20);
        Student s4 = new Student(R.drawable.dog4, "四狗", 21);
        Student s5 = new Student(R.drawable.dog5, "五狗", 22);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    @Override
    public void onBackPressed() {
        long currentPressTime = System.currentTimeMillis();

        if(currentPressTime - lastPressTime > 2000){
            lastPressTime = currentPressTime;
            Toast.makeText(MainActivity.this,
                    "再按一次退出", Toast.LENGTH_SHORT).show();
        }else {
            super.onBackPressed();
        }
    }
}
